--
-- Author:      name
-- DateTime:    2018-08-21 15:57:48
-- Description: 游戏主逻辑

local skynet = require "skynet"
local Timer = import("Timer")
local FSM = import("FSM")


local table_clone = table_clone
local floor = math.floor
local random = math.random


local DeskBase = require "DeskBase"
local Desk = class("Desk",DeskBase)

function Desk:ctor()
	self.super:ctor()

	
end

----------------------------------------------------------------------------------------------
---------开始游戏
---------------------------------------------------------------------------------------------- 
function Desk:start(table_id)
	print("___开始游戏___")

	self.table_id = table_id
	self.node_message = global.node_message	
	self.config = global.config
	self.user_manager = global.user_manager
	self.rule = global.rule


	self.timer = Timer.new()

	--状态时间
	self.stateTime = {	
		rest = 5,		--休息时间
		wait = 3,		--等待
		start = 4,		--开始
		bank = 5,		--抢庄
		bankok = 1,		--抢庄
		bet = 5,		--开始(下注)
		leftcard = 5,	--剩余牌
		settlement = 8, --结算时间
	}
	--游戏状态
	--休息，开始玩家操作，抢庄, 下注，发牌，结算
	--state1 --eventName --state2 --callback
	self.stateTable = {
		{"rest", 		"to_start", 			"start", 			function() self:onStart() end },
		{"start", 		"to_settlement",		"settlement", 		function() self:onSettlement() end },
		{"settlement", 	"to_rest", 				"rest", 			function() self:onRest() end },
	}
	--状态机
	self.fsm = FSM.new(self.stateTable)
	self:reset()

	--启动状态机
	self.fsm:set("rest")
	self.fsm:fire("to_start")
end

-------------------------------------------------------------------------------
-- private
-------------------------------------------------------------------------------


--重置数据
function Desk:reset()

end

--把不符合条件的玩家踢走
function Desk:checkUser()
	for k,v in pairs(self.player_list) do 
		print("----------checkUser ", v.chip)
		if not v.ready or v.chip < self.minChip
		 or (v.doNothingNum and v.doNothingNum >= self.kickDoNothingNum)
		  then 	
		  local sql = string.format("delete from user_online where uid = %d;", k)
		  skynet.send("mysql_service", "lua", "execute", sql)
			if  v.chip < self.minChip then 
				local data = {nt_kick_table = {
					user_id = k,
					content = '金币不足请离场！',
					kick_reason = 1
				}}
				self:sendMessage(k,data)
			end			

			if v.doNothingNum and v.doNothingNum >= self.kickDoNothingNum then
				local data = {nt_kick_table = {
					user_id = k,
					content = '您已长时间未操作，将自动退出游戏。',
					kick_reason = 2,
				}}
				self:sendMessage(k,data)
			end
			--设置玩家所在桌子id为0	
			self:sendGate(v, 'update_desk_id', 0)
			self:sitUp(k)
			self.player_list[k] = nil
			self:updatePlayerNum()
			print("拼十踢走掉线 or 没准备好 or 筹码不够 or 太久没有操作#######",k)
		end
	end
end


--桌子相关配置
function Desk:getConfig()
	local bank_rate = {1,2,3}
	local bet_rate = {1,2,3}
	return {time_rest = self.stateTime.rest,
			time_start = self.stateTime.start,
			time_card = self.stateTime.card,
			time_end = self.stateTime.settlement,
			bank_rate = bank_rate,
			bet_rate = bet_rate,
			room_type = 1,
			bottom_chip = self.bottom_chip, --底分
	}
end

--更新大厅中传来的玩家数据
--updateUserInfo
--disconnect
--getPlayerInfo
--checkAddRobot
--checkRemoveRobot
function Desk:enterDeskCallback(user_id)
	--坐下
	--检查游戏状态
	--是否要加入机器人

end

--离开桌子
function Desk:leaveDeskCallback(user_id)
	--站起
	--从user_manager中删除
	--发送桌子信息变化到zoneserver
end
function Desk:disconnectCallback(user_id)
	--如果不在游戏中
		--站起
		--从user_manager中删除	
end
function Desk:deskStatusCallback(user_id)
	
end




-------------------------------------------------------------------------------
-- 状态
-------------------------------------------------------------------------------


--休息
function Desk:onRest()
	debug_log("_______ps onRest")
	self.operStartTime = os.time()

	--重置数据
	self:reset()
	-- self:ntRest()
	self:removeDisconnect()	

	local callback = function ( ... )		
		self.fsm:fire("to_start")		
	end
	self:setTimer(self.stateTime.rest, callback)
end

--开始
function Desk:onStart()
	debug_log("_______ps onStart")		

	-- self:ntStart()

	local callback = function ()
		self.fsm:fire("to_settlement")
	end
	self:setTimer(self.stateTime.start, callback)

end

--结算
function Desk:onSettlement()
	debug_log("_______ps onSettlement")


				
	local callback = function ()
		--休息
		self.fsm:fire("to_rest")
	end
	self:setTimer(self.stateTime.settlement, callback)
	-- self:doRecord()
end
-------------------------------------------------------------------------------
-- 推送消息
-------------------------------------------------------------------------------



-----------------------------------------------------------------------------------
----------------------------------
-- 数据操作
----------------------------------








return Desk